package polypeptide;


/**
 * 对象数组，独有的方法
 *
 */
public class Test {
    public static void main(String[] args) {
        Person[] persons = new Person[5];
        //一个人，两个老师，两个学生，其中一个老师和学生需要执行独特的他们的方法
        persons[0] = new Person("小明",18);
        persons[1] = new Teacher("小红",19,"5000");
        persons[2] = new Teacher("小刚",20,"4000");
        persons[3] = new Student("小花",21,"90");
        persons[4] = new Student("小白",22,"100");
        //方法 1
        //每一个person都执行say方法
        for(Person person : persons){
            person.say();
        }
        System.out.println("===================");
        //执行独特的方法(向下转型）
        Teacher t1 = (Teacher) persons[1];
        t1.teach();
        Student s1 = (Student) persons[3];
        s1.study();
        System.out.println("===================");
        //但是如果数组的对象很多，又怎么调用特殊方法呢？做法如下
        //方法2
        //判断person[i]的运行类型是否是是Student/Teacher，即，用到 instans of
        for (int i = 0; i < persons.length; i++) {
            persons[i].say();
            if(persons[i] instanceof Student){
                ((Student) persons[i]).study();
            }
            if (persons[i] instanceof Teacher){
                ((Teacher) persons[i]).teach();
            }
        }



    }
}

class Person {
    protected String name;
    protected int age;
    public Person(String name , int age){
        this.age = age;
        this.name = name;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void say(){
        System.out.println("I am "+name+",I am "+age+" years old.");
    }
}
class Student extends Person{
    private String score;
    public Student(String name , int age , String score){
        super(name,age);
        this.score = score;
    }

    public void say(){
        System.out.println("I am "+name+",I am "+age+" years old.My score is "+score+".");
    }
    //特有的学习方法
    public void study(){
        System.out.println("I am "+name+",I am "+age+" years old.I am studying.");
    }
}
class Teacher extends Person{
    private String salary;
    public Teacher(String name , int age , String salary){
        super(name,age);
        this.salary = salary;
    }
    public String getSalary() {
        return salary;
    }
    public void setSalary(String salary) {
        this.salary = salary;
    }
    public void say(){
        System.out.println("I am "+name+",I am "+age+" years old.My salary is "+salary+".");
    }
    //特有的教学方法
    public void teach(){
        System.out.println("I am "+name+",I am "+age+" years old.I am teaching.");
    }
}


